<?php
/**
 * Caching mechanizm with shmop
 * 
 * @version:	0.1
 * @package:	core
 * @author:		alex_ez
 * @created:	21 dec 2008
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */

class cacher_mem implements cacher_interface
{
	protected $instance = null;

	public function __construct()
	{
		$this->instance = new memcached(config::instance()->host);
		$this->instance->addServer('localhost',11211);
	}

	public function retrieve($k) {
		return $this->instance->get($k);
	}

	public function store($k,&$v,$ttl=12) {
		return $this->instance->set($k,$v,$ttl);
	}

	public function drop($k) {
		return $this->instance->delete($k);
	}

	public function cleanup() {
		return $this->instance->flush();
	}

	public function info() {
		$info = $this->instance->getStats();
		if(empty($info)) return array('cache_hits'=>'—','cache_misses'=>'—');
		$info = reset($info);

		$renames = array('get_hits'=>'cache_hits','get_misses'=>'cache_misses');
		$result = array();
		foreach ($renames as $k => $l)
			$result[$l] = $info[$k];

		return $result;
	}
}


?>